package org.dyyx.nrest.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.dyyx.nrest.Pet;
import org.dyyx.nrest.annotation.DeleteMapping;
import org.dyyx.nrest.annotation.GetMapping;
import org.dyyx.nrest.annotation.JsonResponse;
import org.dyyx.nrest.annotation.PathVariable;
import org.dyyx.nrest.annotation.PostMapping;
import org.dyyx.nrest.annotation.PutMapping;
import org.dyyx.nrest.annotation.RequestBody;
import org.dyyx.nrest.annotation.RequestMapping;
import org.dyyx.nrest.annotation.RequestParam;
import org.dyyx.nrest.annotation.RestController;
import org.dyyx.nrest.rest.HttpStatus;
import org.dyyx.nrest.rest.ResponseEntity;

import com.alibaba.fastjson.JSONObject;

//默认为单例，singleton = false表示启用多例。
//@RestController(singleton = false)
@RestController
@RequestMapping("/pets")
public class PetController {
	
	
	private static Map<Integer,Pet> pets = new ConcurrentHashMap<Integer,Pet>();
	
    static{
    	
    	Pet pet = new Pet(1,"cat",7);
    	pets.put(pet.getId(),pet);
    	
    	pet = new Pet(2,"tiger",9);
    	pets.put(pet.getId(),pet);

    	
    }
    
    @GetMapping("")
    @JsonResponse
    public ResponseEntity<List<Pet>> listPets() {
    	List<Pet> list = new ArrayList<Pet>(); 	
    	list.addAll(pets.values()); 	
        return ResponseEntity.ok().build(list);
    }
    
    @GetMapping("/getPet")
    @JsonResponse
    public ResponseEntity<Pet> getPet(@RequestParam("id") int id) { 
    	Pet pet = pets.get(id);
    	if(pet==null){
    		return ResponseEntity.notFound().build();
    	}
        return ResponseEntity.ok().build(pet);
    }
    
    @GetMapping("/deletePet")
    @JsonResponse
    public ResponseEntity<Pet> deletePet(@RequestParam("id") int id) { 
    	Pet pet = pets.remove(id);
    	if(pet==null){
    		return ResponseEntity.notFound().build();
    	}
        return ResponseEntity.ok().build(pet);
    }
    
    @GetMapping("/addOrUpdatePet")
    @JsonResponse
    public ResponseEntity<Pet> addOrUpdatePet(@RequestParam("id") int id,@RequestParam("name") String name,@RequestParam("age") int age) { 
    	Pet pet = new Pet(id,name,age);
    	pets.put(pet.getId(), pet);
        return ResponseEntity.ok().build(pet);
    }
    
    
    
    @PutMapping("/{id}")
    public ResponseEntity<?> putMethod(@PathVariable("id") int id, @RequestBody String body) {
        // update
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteMethod(@PathVariable int id) {
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    
    @PostMapping("")
    public ResponseEntity<?> postMethod(@RequestBody String body) {
        JSONObject json = JSONObject.parseObject(body);
        Pet user = new Pet();
        user.setId(json.getIntValue("id"));
        user.setName(json.getString("name"));
        user.setAge(json.getIntValue("age"));
        return ResponseEntity.status(HttpStatus.CREATED).build(user);
    }

}
